package com.hmall.search.mq;


import com.hmall.search.service.IsearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.hmall.common.pojo.MQConstants.*;

@Component
public class listener {

    @Autowired
    private IsearchService searchService;

    //新增和更新队列消费者
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = ITEM_INSERT_QUEUE),
                    exchange = @Exchange(name = ITEM_EXCHANGE, type = ExchangeTypes.DIRECT),
                    key = ITEM_INSERT_KEY
            )
    )//AMQP注解声明交换机，队列
    //队列绑定消费方法
    public void insertListener(Long id) {//方法参数接收消息内容
        searchService.addItem(id);
    }

    //删除文档消费者
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = ITEM_DELETE_QUEUE),
                    exchange = @Exchange(name = ITEM_EXCHANGE,type = ExchangeTypes.DIRECT),
                    key = ITEM_DELETE_KEY
            )
    )
    public void deleteListener(Long id){
        searchService.deleteItem(id);
    }
}
